package com.shd.edanpei.presenter;

import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.shd.edanpei.ui.activity.CustomerInfoActivity;
import com.shd.edanpei.base.data.OrderConstant;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.ui.activity.PerfectDataActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.IdentityBean;
import com.shd.edanpei.presenter.contract.CertificationContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.util.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


public class CertificationPresenter extends BasePresenterImpl<CertificationContract.CertificationIView>
        implements CertificationContract.CertificationIPresenter {

    private String front_idcard_pic;
    private String back_idcard_pic;

    public CertificationPresenter(CertificationContract.CertificationIView mView) {
        super(mView);
    }

    @Override
    public void uploadPic(final String picPath, final int pic) {
        ApiCreator.getApi().uploadFile(createFileBody(picPath), createStrBody(DaoManager.getInstance().getUserBean().getId() + ""), createStrBody("identitiy" + pic))
                .compose(RxTransformer.<String>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<String>() {
                    @Override
                    protected void onSuccess(String data) {
                        back_idcard_pic = data;
                        DaoManager.getInstance().setBackIdCard(back_idcard_pic);
                        mBaseIView.setPic(data, pic);
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        super.onFailure(throwable);
                        mBaseIView.updateFail(pic);
                    }
                });
    }

    @Override
    public void uploadCertificationPic(String picPath, final int pic) {
        ApiCreator.getApi().uploadIdentityPic(createFileBody(picPath), createStrBody(DaoManager.getInstance().getUserBean().getId() + ""), createStrBody("identitiy" + pic), 1)
                .compose(RxTransformer.<IdentityBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<IdentityBean>() {
                    @Override
                    protected void onSuccess(IdentityBean data) {
                        if (TextUtils.isEmpty(data.getErrorMsg())) {
                            front_idcard_pic = data.getUrl();
                            DaoManager.getInstance().setFrontIdCard(front_idcard_pic);
                            mBaseIView.setIdentitiyInfo(data);
                        } else {
                            ToastUtil.showToast(data.getErrorMsg());
                            mBaseIView.updateFail(pic);
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        super.onFailure(throwable);
                        mBaseIView.updateFail(pic);
                    }
                });
    }

    @Override
    public void submit(final String name, final String number, final int from) {

        if (TextUtils.isEmpty(front_idcard_pic)) {
            ToastUtil.showToast("请上传身份证正面");
            return;
        }
        if (TextUtils.isEmpty(back_idcard_pic)) {
            ToastUtil.showToast("请上传身份背面");
            return;
        }

        List<String> list = new ArrayList<>();
        list.add(front_idcard_pic);
        list.add(back_idcard_pic);
        String s = new Gson().toJson(list);
        String b = s.toString();
        int a = 1;
        mBaseIView.showLoading("提交中...");
        ApiCreator.getApi().identity(name, number, new Gson().toJson(list))
                .compose(RxTransformer.<UserBean>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<UserBean>() {
                    @Override
                    protected void onSuccess(UserBean data) {
                        //认证成功
                        ToastUtil.showToast("认证成功");
                        DaoManager.getInstance().setIsReal(data.isReal());
                        DaoManager.getInstance().setRealName(data.getRealName());
                        if (DaoManager.getInstance().getUserBean().getRoleType() == RoleEnum.bank.getId()) {
                            //进入完善信息
                            mBaseIView.startActivity(PerfectDataActivity.class);
                        }
                        mBaseIView.getContext().finish();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mBaseIView.stopLoading();
                    }
                });
    }

    @Override
    public void isReal() {
        if (DaoManager.getInstance().getUserBean().getRoleType() == RoleEnum.bank.getId()) {
            //进入完善信息
            mBaseIView.startActivity(PerfectDataActivity.class);
            mBaseIView.getContext().finish();
        }

    }

    @Override
    public void toCustomer(Bundle bundle) {
        front_idcard_pic=DaoManager.getInstance().getUserBean().getFrontIdcardPic();
        back_idcard_pic=DaoManager.getInstance().getUserBean().getBackIdcardPic();
        if (TextUtils.isEmpty(front_idcard_pic)) {
            ToastUtil.showToast("请上传身份证正面");
            return;
        }
        if (TextUtils.isEmpty(back_idcard_pic)) {
            ToastUtil.showToast("请上传身份背面");
            return;
        }
        List<String> list = new ArrayList<>();
        list.add(front_idcard_pic);
        list.add(back_idcard_pic);
        String pic = new Gson().toJson(list);
        OrderConstant.ID_PIC = pic;
        mBaseIView.startActivity(CustomerInfoActivity.class, bundle);
        mBaseIView.getContext().finish();
    }

    private MultipartBody.Part createFileBody(final String pic) {
        File file = new File(pic);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
        return MultipartBody.Part.createFormData("file", file.getName(), requestBody);
    }

    private RequestBody createStrBody(final String value) {
        return RequestBody.create(MediaType.parse("text/plain"), value);
    }

}
